package cn.admobiletop.adsuyi.adapter.toutiao.data;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;

import cn.admobiletop.adsuyi.ad.data.ADSuyiRewardVodAdInfo;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiRewardVodAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.CustomDownloadListener;

/**
 * @author ciba
 * @description 激励视频广告内容对象
 * @date 2020/4/2
 */
public class RewardVodAdInfo extends BaseAdInfo<ADSuyiRewardVodAdListener, TTRewardVideoAd> implements ADSuyiRewardVodAdInfo, TTRewardVideoAd.RewardAdInteractionListener {
    private boolean hasShown;
    private Handler handler = new Handler(Looper.getMainLooper());

    public RewardVodAdInfo(String platformPosId) {
        super(platformPosId);
    }

    @Override
    public void setAdapterAdInfo(TTRewardVideoAd ttRewardVideoAd) {
        super.setAdapterAdInfo(ttRewardVideoAd);

        if (getAdapterAdInfo() != null) {
            getAdapterAdInfo().setShowDownLoadBar(true);
            if (TTAdConstant.INTERACTION_TYPE_DOWNLOAD == getAdapterAdInfo().getInteractionType()) {
                getAdapterAdInfo().setDownloadListener(new CustomDownloadListener());
            }
            getAdapterAdInfo().setRewardAdInteractionListener(this);
        }
    }

    @Override
    public void showRewardVod(Activity activity) {
        if (activity != null
                && !isReleased()
                && isReady()
                && getAdapterAdInfo() != null
                && !hasShown()
                && !hasExpired()) {
            hasShown = true;
            getAdapterAdInfo().showRewardVideoAd(activity);
        }
    }

    @Override
    public boolean hasShown() {
        return hasShown;
    }

    @Override
    public boolean hasExpired() {
        return false;
    }

    @Override
    public boolean isReady() {
        return true;
    }

    @Override
    public void onAdShow() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onAdExpose(RewardVodAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onAdVideoBarClick() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onAdClick(RewardVodAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onAdClose() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onAdClose(RewardVodAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onVideoComplete() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onVideoComplete(RewardVodAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onVideoError() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onVideoError(RewardVodAdInfo.this, new ADSuyiError(-1, "unknown"));
                    }
                }
            });
        }
    }

    @Override
    public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int i1, String s1) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onReward(RewardVodAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onSkippedVideo() {

    }

    @Override
    public void releaseAdapter() {
        super.releaseAdapter();
        setAdapterAdInfo(null);

        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
